package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Employee;
import com.ruoyi.system.domain.WageSlipInfo;
import com.ruoyi.system.domain.vo.WageSlipInfoVo;
import com.ruoyi.system.service.IEmployeeService;
import com.ruoyi.system.service.IWageSlipInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.ruoyi.common.utils.PageUtils.startPage;

@Controller
@RequestMapping("/system/wageslipinfo")
public class MyWageSlipInfoController extends BaseController {
    private String prefix = "system/info";

    @Autowired
    private IWageSlipInfoService wageSlipInfoService;

    @Resource
    private IEmployeeService employeeService;


    @GetMapping()
    public String info() {
        return prefix + "/wageslipinfo";
    }
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WageSlipInfo wageSlipInfo) {
        startPage();
        Long userId = ShiroUtils.getSysUser().getUserId();
        SysUser sysUser = ShiroUtils.getSysUser();
        if (!sysUser.isAdmin()){
            wageSlipInfo.setEmployeeId(sysUser.getEmpId());
        }
        List<WageSlipInfo> list = wageSlipInfoService.selectWageSlipInfoList(wageSlipInfo);
        List<WageSlipInfoVo> wageSlipInfoVos = new ArrayList<>();
        list.forEach(ws -> {
            Employee employee = employeeService.selectEmployeeByEmployeeId(ws.getEmployeeId());
            WageSlipInfoVo wageSlipInfoVo = new WageSlipInfoVo();
            BeanUtils.copyProperties(ws,wageSlipInfoVo);
            wageSlipInfoVo.setEmployee(Optional.ofNullable(employee).map(Employee::getName).orElse("关联员工已被删除..."));
            wageSlipInfoVos.add(wageSlipInfoVo);
        });
        return getDataTable(wageSlipInfoVos);
    }
}
